package com.bite.springbook.Controller;

import com.bite.springbook.Model.*;
import com.bite.springbook.Service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;

    /*
    添加图书
     */
    @RequestMapping(value = "/addBook", produces = "application/json")
    public String insertBook(BookInfo bookInfo){
        //校验参数
        log.info("接收到的参数：{}", bookInfo);
        if(!StringUtils.hasLength(bookInfo.getBookName()) ||
                !StringUtils.hasLength(bookInfo.getAuthor()) ||
                !StringUtils.hasLength(bookInfo.getPublish()) ||
                bookInfo.getCount() == null ||
                bookInfo.getPrice()==null ||
                bookInfo.getStatus()==null){
            return "输入参数为空";
        }
       try {
           Integer result = bookService.addBook(bookInfo);
           if(result > 0){
               return "";
           }
       }catch (Exception e){
           log.info("添加失败 e: " + e);

       }
       return "添加失败";

    }

    /*
    查询图书
     */
    @RequestMapping("/getBookListByPage")
    public Result<PageResult<BookInfo>> getBookListByPage(PageRequest pageRequest, HttpSession session){
//        log.info("接收到达参数：{}" + pageRequest);
//        if(session.getAttribute(Constants.SESSION_USER_KEY) == null){
//            return Result.unlogin();
//        }

        PageResult<BookInfo> bookListByPage = bookService.getBookListByPage(pageRequest);
        return Result.success(bookListByPage);
    }

    /*
    根据id获取图书

     */
    @RequestMapping("/getBookById")
    public BookInfo getBookById(Integer bookId){
        log.info("接受到的参数id：" + bookId);
        return bookService.getBookById(bookId);
    }

    /*
    修改图书
     */
    @RequestMapping(value = "/updateBook", produces = "application/json")
    public String updateBook(BookInfo bookInfo){
        log.info("接收到的参数：{}" + bookInfo);
        try{
            Integer result = bookService.updateBook(bookInfo);
            if(result > 0){
                return "";
            }else {
                return "修改失败";
            }
        }catch (Exception e){
            log.info("修改失败" + e);
            return "修改失败";
        }

    }

    /*
    删除图书
     */
    @RequestMapping(value = "/deleteBook", produces = "application/json")
    public String deleteBook(Integer id){
        log.info("接收到的参数id：" + id);
        try {
            Integer result = bookService.deleteBook(id);
            if(result > 0){
                return "";
            }else {
                return "删除失败";
            }
        } catch (Exception e){
            log.info("删除失败：" + e);
            return "删除失败";
        }
    }

    /*
    批量删除
     */
    @RequestMapping(value = "/batchDeleteBook",produces = "application/json")
    public String batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书 + {}" + ids);
        try {
            Integer result = bookService.batchDeleteBook(ids);
            if(result > 0){
                return "";
            }else {
                return "删除失败";
            }
        } catch (Exception e){
            log.info("删除失败：" + e);
            return "删除失败";
        }
    }

}
